package com.twotiger.and.adapter;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.Constants;
import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.account.AccountPage;
import com.twotiger.and.activity.account.capitalflow.CapitalFlowPage;
import com.twotiger.and.activity.account.invested.InverstedRecordPage;
import com.twotiger.and.activity.account.investtransfer.InvestTransferPage;
import com.twotiger.and.activity.account.repayplan.RepayPlanCalendarPage;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.bean.ConstantDataUnit;
import com.twotiger.and.bean.UserAsset;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.PromptManager.ReChargeClickListener;
import com.twotiger.and.util.ViewUtils;
import com.twotiger.and.view.BadgeView;

/**
 * 我的钱、功能按钮
 * @Description: TODO(这里描述这个类的作用) 
 * @Author douxy
 * @Date 2015年8月19日 下午12:05:33
 */
public class AccountOptionsAdapter extends BaseAdapter {
    private String optionsTitle[] = { "充值", "提现","资金流水", "还款日历", "邀请好友", "优惠券", "直投项目", "投资转让", "老虎活期" };
    private int icon[] = { R.drawable.recharge, R.drawable.getcash, R.drawable.amountflow, R.drawable.repayment, R.drawable.myinvitation,
                          R.drawable.coupon, R.drawable.inverstedpro, R.drawable.investtrans,
                          R.drawable.current };
    private LayoutInflater inflater;
    private UserAsset asset;
    private HomeActivity context;
    private BaseHandler handler;
    private HashMap<String, String> parms;
    private boolean isopenthrid;
    private final static int OPENPAYMENTSUCESS = 0;
    private final static int OPENPAYMENTFAIL = 1;
    private final static int GETQUBANKSUCCES = 8;
    private final static int GETQUBANKFAIL = 9;
    private final static int GETCUNBANKSUCCES = 4;
    private final static int GETCUNBANKFAIL = 5;
    private BadgeView badge1;
    private int unusedNum;

    public AccountOptionsAdapter(Context context, UserAsset asset, BaseHandler handler,
                                 boolean isopenthrid, int num) {
        super();
        if (context != null) {
            inflater = LayoutInflater.from(context);
            this.context = (HomeActivity) context;
            parms = this.context.getParm();
        }
        this.asset = asset;
        this.handler = handler;
        this.isopenthrid = isopenthrid;
        this.unusedNum = num;

    }

    @Override
    public int getCount() {
        return optionsTitle.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     * 显示未使用数字
     * 
     * @param view
     */
    public void createTips(View view) {
        if (badge1 == null) {
            badge1 = new BadgeView(context, view);
        }
        if (asset != null && asset.unusedNum > 0) { //数量大于0才显示优惠券数量
            badge1.setText(asset.unusedNum + "");
            badge1.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
            badge1.setTextColor(Color.WHITE);
            badge1.setBadgeBackgroundColor(Color.RED);
            badge1.setTextSize(10);
            badge1.setBadgeMargin(badge1.dipToPixels(38), badge1.dipToPixels(35)); //数字的位置
            badge1.toggle();
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = inflater.inflate(R.layout.grid_option_item, null);
        ImageView item_option_icon = (ImageView) view.findViewById(R.id.item_option_icon);
        TextView item_option_text = (TextView) view.findViewById(R.id.item_option_text);
        ImageView iv_tip = (ImageView) view.findViewById(R.id.iv_tip);
        final RelativeLayout ll_root = (RelativeLayout) view.findViewById(R.id.ll_root);
        item_option_icon.setImageResource(icon[position]);
        item_option_text.setText(optionsTitle[position]);
        if (position == 5) {
            createTips(ll_root);
        }
        if (position == 4) {
            iv_tip.setVisibility(View.VISIBLE);
        } else {
            iv_tip.setVisibility(View.GONE);
        }
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ViewUtils.isFastDoubleClick(v)) {
                    return;
                }
                Intent intent = new Intent();
                switch (position) {
                case 0: //充值
                case 1:
                    if (!isopenthrid) {
                        PromptManager.showConfirmAlertCommon(context, 0, "",
                            "您还未开通汇付天下托管账号，继续开通", "取消", "继续", true, new ReChargeClickListener() {

                                @Override
                                public void onClickConfirm() {

                                }

                                @Override
                                public void onClickCancel() {
                                    // 确认，则继续
                                    parms.clear();
                                    parms.put("token", context.getToken());
                                    context.getDate(parms, Constants.REGISTHF, handler,
                                        OPENPAYMENTSUCESS, OPENPAYMENTFAIL, true, true, false);
                                }
                            });

                        return;
                    }
                    if (position == 0) {
                        //充值
                        parms.clear();
                        parms.put("token", context.getToken());
                        context.getDate(parms, Constants.RECHARGEBANKQUERY, handler,
                            GETCUNBANKSUCCES, GETCUNBANKFAIL, true, true, false);
                    } else {

                        //提现
                        parms.clear();
                        parms.put("token", context.getToken());
                        context.getDate(parms, Constants.WITHDRAWBANKQUERY, handler,
                            GETQUBANKSUCCES, GETQUBANKFAIL, true, true, false);
                    }
                    return;
                case 2:
                    //资金流水
                    intent.setClass(context, CapitalFlowPage.class);

                    break;
                case 3:
                    //还款计划 
                    intent.setClass(context, RepayPlanCalendarPage.class);

                    break;
                case 4:
                    //我的邀请
                    ConstantDataUnit yqdz = context.getConstantDataByKey(Constants.dz_yqhy);
                    if(yqdz != null){
                        intent.setClass(context, WebViewPage.class);
                        intent.putExtra("title", "邀请好友");
                        intent.putExtra("url",  yqdz.getVal()+ "?token=" + context.getToken());
                        intent.putExtra("BACK_MODE", WebViewPage.BACK_DIRECT); //webview的返回形式
                    }

                    break;
                case 5:

                    //优惠券
                    ConstantDataUnit yhq = context.getConstantDataByKey(Constants.dz_yhq);
                    if(yhq != null){
                        intent.setClass(context, WebViewPage.class);
                        intent.putExtra("title", "优惠券");
                        intent.putExtra("url", yhq.getVal() + "?token=" + context.getToken());
                        intent.putExtra("BACK_MODE", WebViewPage.BACK_DIRECT); //webview的返回形式
                    }

                    break;
                case 6:

                    //老虎直投
                    intent.setClass(context, InverstedRecordPage.class);

                    break;
                case 7:
                    //投资转让
                    intent.setClass(context, InvestTransferPage.class);

                    break;
                case 8:
                    if (!isopenthrid) {
                        PromptManager.showConfirmAlertCommon(context, 0, "",
                            "您还未开通汇付天下托管账号，继续开通", "取消", "继续", true, new ReChargeClickListener() {

                                @Override
                                public void onClickConfirm() {

                                }

                                @Override
                                public void onClickCancel() {
                                    // 确认，则继续
                                    parms.clear();
                                    parms.put("token", context.getToken());
                                    context.getDate(parms, Constants.REGISTHF, handler,
                                        OPENPAYMENTSUCESS, OPENPAYMENTFAIL, true, true, false);
                                }
                            });

                        return;
                    }
                    //老虎活期
                    parms.clear();
                    parms.put("token", context.getToken());
                    context.getDate(parms, Constants.ASSETXQUERY, handler,
                        AccountPage.GETCURRENTPROJECTXACOUNTSUCCES,
                        AccountPage.GETCURRENTPROJECTXACOUNTFAIL, true, true, false);

                    return;
                default:
                    break;

                }
                context.startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
            }
        });
        return view;
    }

}
